﻿/// <reference path="vswd-ext_2.2.js" />
/********************poi.js*********************************
*Create Date: 2012-3
*Description : this js file use ext js to create AJAX message function window 
*********************************************************************/

function MessageWindow() {
    var pagesize = 20;
    var fields = ["sn_imei_id", "message_r_datetime", "message_level", "message_body", "send_or_received"];
    var store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchGprsMessage",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: fields,
            root: "data",
            totalProperty: "totalCount"
        })
    });
    var trackNameCombo = getTrackerCheckboxCombo(true);
    trackNameCombo.setWidth(200);
    var day = new Date();
    var today = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate();
    var fromDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        fieldLabel: _RFIDResource_StartTime,
        value: today
    });
    var fromTimeEditor = new Ext.form.TimeField({
        value:"00:00",
        format: 'H:i',
        width: 70
    });
    var toDateEditor = new Ext.form.DateField({
        renderer: Ext.util.Format.dateRenderer('Y-m-d'),
        format: 'Y-m-d',
        fieldLabel: _Alarm_Header_EndTime,
        value: today
    });
    var toTimeEditor = new Ext.form.TimeField({
        value: "23:59",
        format: 'H:i',
        width: 70
    });
    
    var controlMethodCombo = new Ext.ux.form.CheckboxCombo({
        name: 'trackname_combox',
        store: new Ext.data.ArrayStore({
            fields: ['val', 'text'],
            data: [[0, _SMSResource_Send], [1, _SMSResource_Receive]]
        }),
        mode: 'local',
        displayField: 'text',
        width: 120,
        valueField: 'val',
        enableKeyEvents: true,
        fieldLabel: _SMSResource_InOutType,
        editable: false
    });

    var messageLevelCombo = new Ext.ux.form.CheckboxCombo({
        name: 'trackname_combox',
        store: new Ext.data.ArrayStore({
            fields: ['val', 'text'],
            data: [[0, _TrackerMain_NormalMessage], [1, _TrackerMain_UrgentMessage]]
        }),
        mode: 'local',
        displayField: 'text',
        width: 120,
        valueField: 'val',
        enableKeyEvents: true,
        fieldLabel: _CustomerConfigResource_ShowType,
        editable: false
    });



    var tbar = new Ext.Toolbar({
        items: [_Alarm_TrackerName + ':',
        trackNameCombo,
        {
            xtype: 'tbseparator', width: 5
        },
        _Alarm_FromTime + ':',
        fromDateEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        fromTimeEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        _Alarm_ToTime + ':',
        toDateEditor,
        {
            xtype: 'tbseparator', width: 5
        },
        toTimeEditor,
        {
            xtype: 'tbseparator', width: 5
        },controlMethodCombo,{
            xtype: 'tbseparator', width: 5
        },messageLevelCombo,
        {
            tooltip: { text: _TrackerMain_Search, autoHide: true },
            icon: 'images/search16.gif',
            handler: function() {
                store.reload({
                    params: {
                        snids: trackNameCombo.getValue(),
                        fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
                        totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
                        start: 0,
                        limit: pagesize,
                        method: controlMethodCombo.getValue(),
                        level: messageLevelCombo.getValue(),
                        onlyNoCompleted:0
                    }
                });
            }
        }
        ]
    });


    var grid = new Ext.grid.GridPanel({
        store: store,
        columns: [{
            header: _ImageButtonText_TrackerName,
            width: 180,
            sortable: true,
            menuDisabled: true,
            dataIndex: 'sn_imei_id',
            renderer: function(value) {
                var count = trackNameCombo.getStore().getCount();
                for (var i = 0; i < count; i++) {
                    var id = trackNameCombo.getStore().getAt(i).get('snid');
                    if (id == value) {
                        return trackNameCombo.getStore().getAt(i).get('trackname');
                    }
                }
                return "";
            }
        }, {
            header: _Alarm_Header_GPSTime,
            width: 150,
            sortable: true,
            menuDisabled: true,
            dataIndex: 'message_r_datetime'
        }, {
            header: _TrackerMain_Text,
            width: 485,
            sortable: true,
            menuDisabled: true,
            dataIndex: 'message_body'
        }, {
            header: _CustomerConfigResource_ShowType,
            width: 105,
            sortable: true,
            menuDisabled: true,
            dataIndex: 'message_level',
            renderer: function(value) {
                if (value == 1)
                    return _TrackerMain_UrgentMessage;
                else
                    return _TrackerMain_NormalMessage;
            }
        }, {
            header: _SMSResource_InOutType,
            width: 120,
            sortable: true,
            menuDisabled: true,
            dataIndex: 'send_or_received',
            renderer: function(value) {
                if (value == 0)
                    return _SMSResource_Send;
                else
                    return _SMSResource_Receive;
            }
        }
            ],
        autoWidth: true,
        height: 300
    });

    //    grid.addListener('rowdblclick', function(obj, index) {
    //        var record = grid.getStore().getAt(index);
    //        var name = record.get("name");
    //        var longitude = parseFloat(record.get("longitude"));
    //        var latitude = parseFloat(record.get("latitude"));
    //        var iconindex = parseInt(record.get("iconindex"));
    //        var poiid = parseInt(record.get("poiid"));
    //        
    //        if (imap != null) {
    //            imap.drawPOI(poiid, name, latitude, longitude, iconindex, true);
    //        }

    //    });
    var window = new Ext.Window({
        title: _CommandExpression_GPRS_Message,
        layout: 'fit',
        height: 370,
        width: 1120,
        items: grid,
        tbar: tbar,
        constrain:true,
        
        minimizable: true,
         listeners: {
            'minimize': function() {
                addTaskButtonForWindow(window);
            }
        },
        bbar: new Ext.PagingToolbar({
            pageSize: pagesize,
            store: store,
            beforePageText: _Alarm_Page,
            refreshTex: _Alarm_Refresh,
            displayMsg: _Alarm_Displaying,
            emptyMsg: _Alarm_Alarm_emptyMsg,
            displayInfo: true
        })
    });
    store.on('beforeload', function(store) {
        store.baseParams = {
            start: 'start',
            limit: 'limit',
            snids: trackNameCombo.getValue(),
            fromtime: fromDateEditor.getEl().dom.value + ' ' + fromTimeEditor.getEl().dom.value,
            totime: toDateEditor.getEl().dom.value + ' ' + toTimeEditor.getEl().dom.value,
            method: controlMethodCombo.getValue(),
            level: messageLevelCombo.getValue()
        }
    });
    window.show();
}